﻿using System;
using System.Globalization;
using System.IO;
using System.Text;

using FireBus.Contracts;
using FireBus.Shell.Contracts;


namespace FireBus.Shell.FileManager
{
	public class Extension :
		IRequestListener<CommandRequest, CommandResponse>,
		IRequestListener<CommandListRequest, CommandListResponse>
	{
		public CommandResponse OnRequest(CommandRequest request)
		{
			if (request == null)
			{
				throw new ArgumentNullException("request");
			}
			if (!"dir".Equals(request.Command))
			{
				return new CommandResponse(
					"unknown command");
			}
			var directory = request.Arguments.Count > 0 ? request.Arguments[0] : @"c:\";
			if (!Directory.Exists(directory))
			{
				return new CommandResponse(
					String.Format(
						CultureInfo.CurrentCulture,
						"Directory {0} does not exist.",
						directory));
			}
			var builder = new StringBuilder();
			var files = Directory.GetFiles(directory);
			foreach (var file in files)
			{
				builder.AppendLine(file);
			}
			return new CommandResponse(
				builder.ToString());
		}

		public CommandListResponse OnRequest(CommandListRequest request)
		{
			return new CommandListResponse(
				new []
					{
						new ShellCommandToken(
							"dir",
							"Prints out the content of specified directory"),
					});
		}
	}
}
